RedAlph_LoadLibrary <- function() {
  #load libraries
  lib <- c("parallel","BioPhysConnectoR","multicore","seqinr")
  for (i in 1:length(lib)) {
    E<-lib[i]
    if(!(paste0("package:",lib[i],sep="") %in% search())){
    if(!require(E,character.only=T)) {
      if(!isTRUE(install.packages(E))) {print(paste("Please install the package per 'R CMD INSTALL':",lib[i]))}
    }
  } 
  }
} 

Spec_iGEM_ReducingAlphabet.R<-function(){}
source("iGEM_Analysis.R")
RedAlph_LoadLibrary()

dAA<-data.frame(Alphabet=rep("Norm",20),AA=c("A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R","S","T","V","W","Y"),RA3=c("A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R","S","T","V","W","Y"),Property=c("Ala","Cys","Asp","Glu","Phe","Gly","His","Ile","Lys","Leu","Met","Asn","Pro","Gln","Arg","Ser","Thr","Val","Trp","Tyr")) 
dHP<-data.frame(Alphabet=rep("HP",2),AA=c("AGTSNQDEHRKP","CMFILVWY"),RA3=c("P","H"),Property=c("Hydrophilic","Hydrophobic"))
dCSI<-data.frame(Alphabet=rep("CSI",5),AA=c("IVL","FYWH","KRDE","GACS","TMQNP"),RA3=c("A","R","C","T","D"),Property=c("Aliphaetic","Aromatic","Charged","Tiny","Diverse"))
dCSII<-data.frame(Alphabet=rep("CSII",6),AA=c("IVL","FYWH","KR","DE","GACS","TMQNP"),RA3=c("A","R","P","N","T","D"),Property=c("Aliphaetic","Aromatic","Pos. charged","Neg. charged","Tiny","Diverse"))
dM15<-data.frame(Alphabet=rep("M15",15),AA=c("LVIM","C","A","G","S","T","P","FY","W","E","D","N","Q","KR","H"),RA3=c("L","C","A","G","S","T","P","F","W","E","D","N","Q","K","H"),Property=c("Large hydrophobic","","","","","","","Hydrophobic/aromatic sidechains","","","","","","Long-chain positively charged",""))
dM10<-data.frame(Alphabet=rep("M10",10),AA=c("LVIM","C","A","G","ST","P","FYW","EDNQ","KR","H"),RA3=c("L","C","A","G","S","P","F","E","K","H"),Property=c("Large hydrophobic","","","","Polar","","Hydrophobic/aromatic sidechains","Charged / polar","Long-chain positively charged",""))
dM8<-data.frame(Alphabet=rep("M8",8),AA=c("LVIMC","AG","ST","P","FYW","EDNQ","KR","H"),RA3=c("L","A","S","P","F","E","K","H"),Property=c("Hydrophobic","","Polar","","Hydrophobic/aromatic sidechains","","Long-chain positively charged",""))
dData.Alph<<-rbind(dAA,dHP,dCSI,dCSII,dM15,dM10,dM8)#global

getdData.Alph<-function(){
  return(dData.Alph)
}

RA3_Start<-function(alphabet="",clustalo=F,clustalw2=F) {
  print("Set working directory:")
  setwd(tk_choose.dir())
  print("Count of data expected (integer):")
  E<-readline() #Number of Files
  print("Now choose files")
  if(is.integer(as.integer(E))){#load files
    #Select Data Path
    Path<-NULL
    Name<-NULL
    subDir<-NULL
    for (i in 1:E){
      Path[i]<-tk_choose.files() 
      Name[i]<-basename(Path[i])  #with extension
      #here create ordner in WD like WD/Data/... <- in Data files of Data
      subDir[i]<-sub("^([^.]*).*", "\\1", Name[i]) ##without extension      
    }
    for (i in 1:E){
      RA3_Files(Path[i],subDir[i],alphabet,clustalo,clustalw2)
    }
}
else{
  print("Error: No integer was submitted. Stopping program.")
}

#reassembling of fasta files possible with clustal i = T
RA3_Files <- function(files,Name,alphabet="",clustalo=F,clustalw2=F){ #multiple sequence with possible realignment after reduction
  for (i in 1:nrow(files$ali)){
    files$ali[i]<-RA3(files$ali[i]) #Modify Fasta.aln[i]<-reduction    
  }
  files<-MSA_Seq(files="",clustalo=F,clustalw2=F)  
  Name<-paste(Name,"-RA3-",as.character(alphabet),".fasta",sep="",collapse=NULL)
  write.fasta(list(files$id,files$ali),file=Name)
}
}

#DIrect Reduction here is string
RA3 <- function(fasta,alphabet=""){ #one sequence
  if (alphabet=="") {
    print("Choose a reducing alphabet!")
  }
  else{
      fasta<-toupper(fasta)
      E<-NULL
      for (i in 1:length(fasta)){ 
        FRA<-fRA3(fasta[i],alphabet)
        E<-cbind(E,FRA)
      }
      E<-as.character(E) 
    }
  return(E)
} 

fRA3<-function(fasta,alphabet){
  Sub<-subset(dData.Alph,dData.Alph$Alphabet==alphabet)
  #Data<-substring(fasta, seq(1,nchar(fasta),1), seq(1,nchar(fasta),1)) 
  Data<-as.vector(unlist(strsplit(fasta,split="")))
  E<-NULL
  gap.char<-"-"
  for (i in 1:nchar(fasta)){
    HIT<-F
    j<-1
    while((!HIT)&&(j<=length(Sub$AA))){
      
      if(!(as.character(Data[i])==gap.char)){
        if(any(grepl(as.character(Data[i]),as.character(Sub$AA[j])))){
          E<-paste0(E,as.character(Sub$RA3[j]),collapse=NULL)
          HIT<-TRUE
        }
      }else{
        E<-paste0(E,as.character(gap.char),collapse=NULL)
        HIT<-TRUE
      }  
      
      j<-j+1
      }
  }
  return(E)  
}
